package com.example.personal.aiagent.chat.commonservice;

import com.example.personal.aiagent.common.constant.ErrorCode;
import com.example.personal.aiagent.xunfei.account.model.dto.AccountDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;

import java.util.Arrays;

// TODO: 以下所有方法异常时均需要直接关闭资源+状态切换至关闭
@Slf4j
public abstract class BaseChatService implements ChatService {
    /**
     * 应用ID
     */
    private final String appId;
    /**
     * 应用secret
     */
    private final String apiSecret;
    /**
     * 应用key
     */
    private final String apiKey;
    /**
     * 状态，初始化成关闭
     */
    protected ChatStatus status = ChatStatus.CLOSED;

    /**
     * 初始化
     *
     * @param accountDTO 账号数据
     */
    public BaseChatService(AccountDTO accountDTO) {
        appId = accountDTO.getAppId();
        apiSecret = accountDTO.getApiSecret();
        apiKey = accountDTO.getApiKey();
    }

    @Override
    public void startChat() {
        log.info("start chat");
        if (status != ChatStatus.CLOSED) {
            throw new ChatException(ErrorCode.CHAT_STATUS_INVALID, Arrays.asList(status.name(),
                "not support start chat"));
        }
        status = ChatStatus.INIT;
        // 构建鉴权url
        try {
            doStartChat();
        } catch (Exception ex) {
            ChatStatus oldStatus = ChatStatus.INIT;
            status = ChatStatus.CLOSED;
            throw new ChatException("", ex, ErrorCode.CHAT_INTERNAL_ERROR, Arrays.asList(oldStatus.name(),
                "startChat error in handler"));
        }
        status = ChatStatus.READY_FOR_CHAT;
    }

    public abstract void doStartChat();

    @Override
    public void inputChatContent(String content) {
        log.debug("input chat content, {}", content);
        if (status != ChatStatus.READY_FOR_CHAT) {
            throw new ChatException(ErrorCode.CHAT_STATUS_INVALID, Arrays.asList(status.name(),
                "not support input chat content"));
        }
        status = ChatStatus.BUSY;
        asyncInputChatContent(content);
    }

    /**
     * 设置聊天分析结束
     */
    protected void setChatAnalyseFinish() {
        if (status != ChatStatus.BUSY) {
            throw new ChatException(ErrorCode.CHAT_STATUS_INVALID, Arrays.asList(status.name(),
                "not support set chat analyse finish"));
        }
        status = ChatStatus.READY_FOR_GET_RESULT;
    }

    /**
     * 异步执行聊天内容
     * 
     * @param content 内容
     */
    public abstract void asyncInputChatContent(String content);

    @Override
    public String getChatResult() {
        log.debug("get chat result");
        if (status != ChatStatus.READY_FOR_GET_RESULT) {
            throw new ChatException(ErrorCode.CHAT_STATUS_INVALID, Arrays.asList(status.name(),
                "not support get chat result"));
        }
        String res = doGetChatResult();
        status = ChatStatus.READY_FOR_CHAT;
        return res;
    }

    public abstract String doGetChatResult();

    @Override
    public void closeChat() {
        log.debug("close chat");
        if (status != ChatStatus.READY_FOR_CHAT) {
            throw new ChatException(ErrorCode.CHAT_STATUS_INVALID, Arrays.asList(status.name(),
                "not support close chat"));
        }
        status = ChatStatus.CLOSING;
        doCloseChat();
        status = ChatStatus.CLOSED;
    }

    public abstract void doCloseChat();

    @Override
    public void forceCloseChat() {
        log.warn("force close chat");
        status = ChatStatus.CLOSING;
        doForceCloseChat();
        status = ChatStatus.CLOSED;
    }

    public abstract void doForceCloseChat();
}
